﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Drawing;

namespace _180FrameWork.CustControls
{
    public partial class CustButtonTextBox : TextBox,ICustControl
    {
        private bool _IsBind = false;
        public bool IsBind { get { return this._IsBind; } protected set { this._IsBind = value; } }

        private string _PropertName = "";
        public string PropertName { get { return this._PropertName; } set { this._PropertName = value; } }

        //private string _QueryName = "";
        //public string QueryName { get { return this._QueryName; } set { this._QueryName = value; } }

        private string _RegexText = "";
        public string RegexText { get { return this._RegexText; } set { this._RegexText = value; } }

        private string _btnText = "";
        public string BtnText { get { return this._btnText; } set { this._btnText = value; this.btn.Text = value; } }
        
        public CustButtonTextBox()
        {
            InitializeComponent();
            Init();
        }

        public void SetValue(object strValue)
        {
            this.Text = strValue.ToString();
        }

        public object GetValue()
        {
            return this.Text;
        }

        public void Binding(object DataSource, string DataMember)
        {
            this.DataBindings.Add("Text", DataSource, DataMember);
            IsBind = true;
        }

        public void Clean()
        {
            this.Text = "";
        }

        public void GetFocus()
        {
            base.Focus();
        }

        private void Control_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
        }

        private void CustTextBox_Validating(object sender, CancelEventArgs e)
        {
            if (this.RegexText != "" && this.Text != "")
            {
                Regex reg = new Regex(this.RegexText);
                if (!reg.IsMatch(this.Text))
                {
                    this.Clean();
                    e.Cancel = true;
                }
            }
        }

        private void CustTextBox_Validated(object sender, System.EventArgs e)
        {

        }
        private Button btn;
        private void Init() {

            btn = new Button();
            btn.Dock = DockStyle.Right;
            btn.Location = new Point(0, 0);
            btn.Name = "btnPop";
            btn.AutoSize = true;
            //btn.Size = new Size(23, 23);
            btn.TabIndex = 0;
            btn.Text = this.BtnText;
            btn.UseVisualStyleBackColor = true;
            btn.Click += new EventHandler(btn_Click);
            this.Controls.Add(btn);
        }

        void btn_Click(object sender, EventArgs e)
        {
            if (BtnClick != null)
            {
                BtnClick(sender, e);
            }
        }

        /// <summary>
        /// 点击事件
        /// </summary>
        [Category("Data")]
        [Description("CustButtonTextBox_BtnClickDescr")]
        //public event EventHandler ItemSelected;
        public event BtnClickEventHandler BtnClick;

        public delegate void BtnClickEventHandler(object sender, EventArgs e);
    }
}
